package com.zone.vehicle.superApps.view.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.zone.lib.download.DownloadStatus;
import com.zone.lib.download.db.DownloadDaoUtilsStore;
import com.zone.lib.download.db.entity.Download;

import org.apache.commons.lang3.StringUtils;

import java.io.File;

/**
 * @Author: pkqlqx
 * @CreateDate: 2021/12/20 18:05
 * @Description:
 */
public class SuperAppReceiver extends BroadcastReceiver {
    public static final String TAG = SuperAppReceiver.class.getName();

    public static final String INSTALL_JINGDONG_ACTION = "com.jingdong.manto.pkg.action";
    private static final String EVENT_DOWNLOADED = "downloaded";
    private static final String EVENT_DELETED = "deleted";
    public static final String TEST_REMOTE_ACTION = "com.superapp.showdialog";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive:"+intent.getAction());
        Toast.makeText(context, "收到广播"+intent.getAction(), Toast.LENGTH_SHORT).show();
        String action = intent.getAction();
        if(TextUtils.equals(action, INSTALL_JINGDONG_ACTION)){
            String event = intent.getStringExtra("event");
            String appId = intent.getStringExtra("appId");
            String appType = intent.getStringExtra("appType");
            if (StringUtils.isNotEmpty(event) && event.equals(EVENT_DOWNLOADED)){
                //小程序下载完成
                Download download = new Download();
                download.setCode(appId);
                download.setType("2");
                download.setSize(-1);
                download.setDownloadLocation(-1);
                download.setDownloadStatus(DownloadStatus.COMPLETE_INSTALL);
                DownloadDaoUtilsStore.getInstance().getDownloadAppDaoUtils().insert(download);
            }else if (StringUtils.isNotEmpty(event) && event.equals(EVENT_DELETED)){
                //小程序删除
                DownloadDaoUtilsStore.getInstance().deleteByCode(appId);
            }
        }else if (TextUtils.equals(action, TEST_REMOTE_ACTION)){

            File dir = new File(context.getCacheDir(), "download");
            if (!dir.exists()){
                dir.mkdirs();
            }
            File saveFile = new File(dir, "1111.apk");

//            DownloadHelper.getInstance().addTask("1111", "url", saveFile, DownloadConstant.DOWNLOAD_APP_ACTION, Content.DOWNLOAD).submit(context);

//            UpdateDialog dialog = new UpdateDialog(context);
//            dialog.setTitle("远程请求安装APP，是否同意");
//            dialog.setSubTitle("什么app");
//            dialog.setPositive("同意");
//            dialog.setNegtive("不同意");
//            dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
//                @Override
//                public void onCancel(DialogInterface dialog) {
//                    dialog.cancel();
//                }
//            });
//            dialog.setOnClickBottomListener(new UpdateDialog.OnClickBottomListener() {
//                @Override
//                public void onPositiveClick() {
//                    Toast.makeText(context, "开始下载安装", Toast.LENGTH_SHORT).show();
//                    Intent startIntent = new Intent(context, SuperAppService.class);
//                    context.startService(startIntent);
//                }
//
//                @Override
//                public void onNegtiveClick() {
//                    dialog.cancel();
//                }
//            });
//            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
//            dialog.show();
        }else if (TextUtils.equals(action, TEST_REMOTE_ACTION)){

        }

    }
}
